<?php
namespace {
/**
 * Enfold Child Theme - Starter
 *
 * @since Enfold Starter 1.0
 *
 * Add your own functions here. You can also copy some of the theme functions into this file. 
 * Wordpress will use those functions instead of the original functions then.
 * 
 * @link http://www.kriesi.at/documentation/enfold/using-a-child-theme/
 */


/**
 *	Add filter to add or replace Enfold ALB shortcodes with new folder contents
 *	
 *	Note that the shortcodes must be in the same format as those in 
 *	enfold/config-templatebuilder/avia-shortcodes
 *
 *	@link http://www.kriesi.at/documentation/enfold/add-new-or-replace-advanced-layout-builder-elements-from-child-theme/
 */

add_filter('avia_load_shortcodes', 'avia_include_shortcode_template', 15, 1);
function avia_include_shortcode_template($paths)
{
	$template_url = get_stylesheet_directory();
    	array_unshift($paths, $template_url.'/shortcodes/');

	return $paths;
}


/**
 *	Turn on Custom CSS Class field for all Avia Layout Builder elements
 *	@link http://www.kriesi.at/documentation/enfold/turn-on-custom-css-field-for-all-alb-elements/
 */
add_theme_support('avia_template_builder_custom_css');
}

namespace ITSociaal\Themes\Houtgrutter
{
	const LIBDIR = __DIR__ . "/lib";
	const SRCDIR = __DIR__ . "/src";

	require_once LIBDIR . '/Autoloader.php';
	class_alias("Paulyg\\Autoloader", __NAMESPACE__ . "\\Autoloader");

	function do_autoloader($namespaces)
	{
		return array_map(
			'call_user_func_array',
			array_fill(
				0,
				count($namespaces),
				[new Autoloader, "addPsr4"]
			),
			$namespaces
		);
	}

	function theme_root_path()
	{
		$ret = __DIR__;

		if (defined("STYLESHEETPATH")) {
			assert(constant("STYLESHEETPATH") === $ret);
		}

		return $ret;
	}

	do_autoloader([
		[__NAMESPACE__, SRCDIR],
		["ErrorDumper", LIBDIR . "/error-dumper/src"],
		//["Cypress\\Curry", LIBDIR . "/php-curry"],
	]);

	if (defined("WP_DEBUG") && constant("WP_DEBUG")) {
		if (!is_admin() && (PHP_SAPI != "cli")) {
			\ErrorDumper\Magic::registerErrorDumper();
		}

		error_reporting(~0);
	}

	if (!isset($_REQUEST["page"])) {
		$_REQUEST["page"] = "";
	}

	\add_filter("posts_results", function($posts) {
		// Fix for wp_query with virtual pages
		if (isset($posts[0])
		&& isset($posts[0]->ID)
		&& (0 == $posts[0]->ID)
		) {
			// Set to some unregistered type to prevent errors
			$posts[0]->post_type =
				strtolower(array_reverse(explode("\\", __NAMESPACE__))[0]);
		}

		return $posts;
	}, 32767);

	\add_filter("kriesi_backlink", F::always(""));
	\add_filter("avf_google_fontlist", F::always(
		"Titillium+Web:600,600italic|Ubuntu:400,400italic"
	));

	\add_action("wp_enqueue_scripts", function() {
		wp_deregister_script("avia-compat");
		wp_deregister_script("avia-default");
		wp_deregister_script("avia-shortcodes");
		wp_deregister_script("avia-popup");

		$base_uri = \get_stylesheet_directory_uri();
		wp_enqueue_script("avia-compat", "$base_uri/assets/avia-compat.js", ["jquery"], 2, false );
		wp_enqueue_script("avia-default", "$base_uri/assets/avia.js", ["jquery"], 3, false);
		wp_enqueue_script("avia-shortcodes", "$base_uri/assets/shortcodes.js", ["jquery"], 3, false);
	}, 1024);

	\add_action("ava_after_main_title", function() {
		if (is_shopp_catalog_frontpage ())
			TemplateController::main("laura/advertentie");
	}, 1024);

	\add_action("wp_head", function() {
		$uri = \get_stylesheet_directory_uri() . "/assets/ie9.css";
		?>

<!--[if IE lte 9]>
	<link rel="stylesheet" href="<?=$uri?>" type="text/css"/>
<![endif]-->

		<?php
	});
}

/* vi:set ts=4 sw=4 noet: */
